package com.icebergteam.warfacewidget.core.settings;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;

import com.icebergteam.warfacewidget.AppManager;
import com.icebergteam.warfacewidget.R;
import com.icebergteam.warfacewidget.WidgetProvider;
import com.icebergteam.warfacewidget.core.LogManager;

public class GeneralSettings extends Settings {
	public static final String className = GeneralSettings.class.getCanonicalName();
	// UI settings
	String m_UILang = "ru";
	// Server settings
	// Region
	boolean autodetectRegion = true;
	private String serverRegion = ".ru";
	private String serverLang = "";
	public final static String PREFS_FILE_NAME = "general_settings.ini";
	
	private String typeCurrentService = WidgetProvider.NEWS_TYPE_LIST;
	
//	boolean downloadWithWiFi = true;
	
	
//	public boolean isDownloadWithWifi(){
//		return this.downloadWithWiFi;
//	}
//	
//	public void setDownloadWithWifi(boolean downloadWithWiFi){
//		this.downloadWithWiFi = downloadWithWiFi;
//	}

	public boolean isAutodetectRegion() {
		return autodetectRegion;
	}

	public void setAutodetectRegion(boolean autodetect) {
		this.autodetectRegion = autodetect;
	}

	public String getServerRegion() {
		return this.serverRegion;
	}

	public void setServerRegion(String serverRegion) throws Exception {
		if (this.serverRegion != serverRegion) {
			this.serverRegion = serverRegion;
			this.serverLang = AppManager.getLocaleManager().getRegionLangs(
					serverRegion)[0];
		}
	}

	public void setServerRegionById(int serverRegionId) throws Exception {
		setServerRegion(AppManager.getLocaleManager().getRegionByIndex(
				serverRegionId));
	}

	public String getServerLang() {
		return this.serverLang;
	}

	public void setServerLang(String serverLang) {
		this.serverLang = serverLang;
	}

	public String getUILang() {
		return this.m_UILang;
	}

	public void setUILang(String uiLang) {
		this.m_UILang = uiLang;
	}
	
	// Hidden
		// adBanner
	long lastAdBannerClickTimestamp = 0;

	public long getLastAdBannerClickTimestamp() {
		return lastAdBannerClickTimestamp;
	}

	public void setLastAdBannerClickTimestamp(long m_lastAdBannerClickTimestamp) {
		this.lastAdBannerClickTimestamp = m_lastAdBannerClickTimestamp;
	}

	public GeneralSettings() throws Exception {
		Context context = AppManager.getContext();
		m_UILang = AppManager.getLocaleManager().getSuppotedUILang();
		init(context);
		load();
	}

	@Override
	public synchronized void save() {
		Context context = AppManager.getContext();
		try {
			FileOutputStream fos = context.openFileOutput(getPrefFileName(),
					Context.MODE_PRIVATE);
			this.properties.setProperty(
					getKey(R.string.key_prefs_general_server_autodetect),
					String.format("%s", this.autodetectRegion));
			this.properties.setProperty(
					getKey(R.string.key_prefs_general_server_region),
					this.serverRegion);
			this.properties.setProperty(
					getKey(R.string.key_prefs_general_server_language),
					this.serverLang);
			this.properties.setProperty(
					getKey(R.string.key_prefs_general_ui_language),
					this.m_UILang);
			this.properties.setProperty(
					getKey(R.string.key_prefs_ad_click_timestamp), 
					String.format("%s", this.lastAdBannerClickTimestamp));
			
			this.properties.setProperty(
					getKey(R.string.key_prefs_type_current_service), 
					String.format("%s", this.typeCurrentService));
						
			this.properties.store(fos, "warfacewidget");
			fos.close();
			
			LogManager.LogV(GeneralSettings.className, LogManager.getExecuteMethodName(),
					this.autodetectRegion, this.serverRegion,
					this.serverLang, this.m_UILang, this.properties, this.typeCurrentService);
		} catch (FileNotFoundException e) {
			LogManager.LogE(GeneralSettings.className, LogManager.getExecuteMethodName() + " can't read pref file",
					e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			LogManager.LogE(GeneralSettings.className, LogManager.getExecuteMethodName() + " can't read pref file",
					e.getMessage());
			e.printStackTrace();
		}
	}

	@Override
	public synchronized void load() throws Exception {
		try {
			String autodetectRegion = this.properties.getProperty(
					getKey(R.string.key_prefs_general_server_autodetect),
					String.format("%s", this.autodetectRegion));
			String serverRegion = this.properties.getProperty(
					getKey(R.string.key_prefs_general_server_region),
					String.format("%s", this.serverRegion));
			String serverLang = this.properties.getProperty(
					getKey(R.string.key_prefs_general_server_language),
					String.format("%s", this.serverLang));
			String UILang = this.properties.getProperty(
					getKey(R.string.key_prefs_general_ui_language),
					String.format("%s", this.m_UILang));
			String lastAdBannerClickTimestamp = this.properties.getProperty(
					getKey(R.string.key_prefs_ad_click_timestamp),
					String.format("%s", this.lastAdBannerClickTimestamp));
			String typeCurrentService = this.properties.getProperty(
					getKey(R.string.key_prefs_type_current_service),
					String.format("%s", this.typeCurrentService));
			
	
			this.autodetectRegion = Boolean.parseBoolean(autodetectRegion);
			this.serverRegion = serverRegion;
			this.serverLang = serverLang;
			this.m_UILang = UILang;
			this.lastAdBannerClickTimestamp = Long.parseLong(lastAdBannerClickTimestamp);
			setTypeCurrentService(typeCurrentService);
			LogManager.LogV(GeneralSettings.className, LogManager.getExecuteMethodName(),
					this.autodetectRegion, this.serverRegion,
					this.serverLang, this.m_UILang, this.typeCurrentService, properties.toString());
		} catch (Exception e) {
			LogManager.LogE(GeneralSettings.className, LogManager.getExecuteMethodName(), e.getMessage());
			e.printStackTrace();
		}
	}

	/**
	 * @return the typeCurrentService
	 */
	public String getTypeCurrentService() {
		return typeCurrentService;
	}

	/**
	 * @param typeCurrentService the typeCurrentService to set
	 */
	public void setTypeCurrentService(String typeCurrentService) {
		this.typeCurrentService = typeCurrentService;
	}

	@Override
	public String getPrefFileName() {
		return GeneralSettings.PREFS_FILE_NAME;
	}
}
